#include "../../../shared/dal/chat_session_member_dao.hpp"
#include "../../../shared/dal/odb_factory.hpp"
#include "../../../entity/chat_session_member.hxx"
#include "chat_session_member-odb.hxx"
#include "logger.hpp"
#include <gflags/gflags.h>
#include <iostream>
#include <memory>
#include <string>
#include <gtest/gtest.h>

shared::dal::ChatSessionMemberDaoPtr chatSessionMember;

TEST(TransmiteDBTest, InsertAndSelect) 
{
    chatSessionMember->insert("session-1", "uid1");
    chatSessionMember->insert("session-1", "uid2");
    chatSessionMember->insert("session-2", "uid3");

    auto members = chatSessionMember->selectMembers("session-1");
    ASSERT_EQ(members[0], "uid1");
    ASSERT_EQ(members[1], "uid2");

    members = chatSessionMember->selectMembers("session-2");
    ASSERT_EQ(members[0], "uid3");

    members = {"uid1", "uid2", "uid3"};
    std::pair<std::string, std::vector<std::string>> memberList({"session-3", members});
    chatSessionMember->insert(memberList);

    members = chatSessionMember->selectMembers("session-3");
    ASSERT_EQ(members[0], "uid1");
    ASSERT_EQ(members[1], "uid2");
    ASSERT_EQ(members[2], "uid3");
}

TEST(TransmiteDBTest, RemoveAndSelect) 
{
    entity::ChatSessionMember member1("session-2", "uid3");
    chatSessionMember->remove(member1);

    auto members = chatSessionMember->selectMembers("session-2");
    for(auto& userId : members) {
        ASSERT_NE(userId, "uid3");
    }

    chatSessionMember->remove("session-3");
    members = chatSessionMember->selectMembers("session-3");
    ASSERT_TRUE(members.empty());
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);

    auto db = shared::dal::ODBFactory::create("root", "weng9244", "127.0.0.1", "chat_system", "utf8", 0, 1);
    
    chatSessionMember = std::make_shared<shared::dal::ChatSessionMemberDao>(db);

    return RUN_ALL_TESTS();
}